import { init, plugins } from '@alilc/lowcode-engine';
import { createFetchHandler } from '@alilc/lowcode-datasource-fetch-handler'
import InjectPlugin from '@alilc/lowcode-plugin-inject';
import EditorInitPlugin from './plugins/plugin-editor-init';

import ComponentPanelPlugin   from '@alilc/lowcode-plugin-components-pane';
import SchemaPlugin from '@alilc/lowcode-plugin-schema';
import ManualPlugin from "@alilc/lowcode-plugin-manual";
import ZhEnPlugin   from '@alilc/lowcode-plugin-zh-en';
import CodeEditorPlugin from "./plugins/plugin-code-editor";
import DataSourcePanePlugin   from '@alilc/lowcode-plugin-datasource-pane';
// import demoPlugin from './plugins/plugin-demo-top';

import LogoSamplePlugin from './plugins/plugin-logo-sample';
import UndoRedoPlugin   from './plugins/plugin-undo-redo';
import SimulatorResizerPlugin from '@alilc/lowcode-plugin-simulator-select';
import DeviceSelect from './plugins/plugin-device-select';
import SaveSamplePlugin from './plugins/plugin-save-sample';
import GenerateCodePlugin from './plugins/plugin-generate-code';
import CodeGenPlugin    from '@alilc/lowcode-plugin-code-generator';
import PreviewSamplePlugin from './plugins/plugin-preview-sample';
import LoadIncrementalAssetsWidgetPlugin from './plugins/plugin-load-incremental-assets-widget';

import SetRefPropPlugin from '@alilc/lowcode-plugin-set-ref-prop';
import DefaultSettersRegistryPlugin from './plugins/plugin-default-setters-registry';
import CustomSetterSamplePlugin from './plugins/plugin-custom-setter-sample';
import SimulatorLocalePlugin from './plugins/plugin-simulator-locale';

import './global.scss';

async function registerPlugins() {
  // 注意 Inject 插件必须在其他插件前注册，且所有插件的注册必须 await
  await plugins.register(InjectPlugin);
  // 项目初始化注册组件：使用 /src/services/assets.json
  await plugins.register(EditorInitPlugin, {
    scenarioName: 'general', // 项目名称
    displayName: '凯源建站引擎',
    info: {
      urls: [
        {
          key: '设计器',
          value: 'https://github.com/alibaba/lowcode-demo/tree/main/demo-general',
        }
      ],
    },
  });
  
  /** area: 'leftArea' **/ 
  // 组件初始化面板，没有组件时不可点击
  await plugins.register(ComponentPanelPlugin);
  
  // 展示 schema
  await plugins.register(SchemaPlugin, { isProjectSchema: true });
  // 打开使用文档（直接跳转走了）
  await plugins.register(ManualPlugin);
  // 中英文切换
  await plugins.register(ZhEnPlugin);
  // 代码编辑面板
  await plugins.register(CodeEditorPlugin);
  
  // 数据操作面板
  // await plugins.register(DataSourcePanePlugin, {
  //   importPlugins: [],
  //   dataSourceTypes: [{type: 'fetch'}, {type: 'jsonp'}]
  // });
  /** area: 'leftArea' **/
 
  /** area: 'topArea' **/
  // 项目logo区域
  await plugins.register(LogoSamplePlugin);
  // 操作历史的前进后退
  await plugins.register(UndoRedoPlugin);
  // 预览效果的设备切换
  // await plugins.register(SimulatorResizerPlugin);
  await plugins.register(DeviceSelect);
  // 保存代码、重置页面（到本地，可以接入数据库）
  await plugins.register(SaveSamplePlugin);
  // 生成最终代码(自用改版)
  await plugins.register(GenerateCodePlugin);
  // 生成最终代码（官方demo）
  // await plugins.register(CodeGenPlugin);
  // 预览（schema直接渲染）
  await plugins.register(PreviewSamplePlugin);
  // 动态添加组件（这是个奇葩的设计）
  // await plugins.register(LoadIncrementalAssetsWidgetPlugin);
  /** area: 'topArea' **/

  // await plugins.register(SetRefPropPlugin);
  await plugins.register(DefaultSettersRegistryPlugin);
  return;
  await plugins.register(CustomSetterSamplePlugin);
  await plugins.register(SimulatorLocalePlugin);

};

(async function main() {
  await registerPlugins();

  init(document.getElementById('lce-container')!, {
    locale: 'zh-CN',
    enableCondition: true,
    enableCanvasLock: true,
    // 默认绑定变量
    supportVariableGlobally: true,
    requestHandlersMap: {
      fetch: createFetchHandler(),
    },
  });
})();


